<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "order".
 *
 * @property integer $id
 * @property integer $product_id
 * @property double $unit_price
 * @property integer $sale_num
 * @property double $order_price
 * @property double $import_duty
 * @property double $export_tax
 * @property integer $status
 * @property string $remark
 * @property integer $provider_id
 * @property integer $provider_member_id
 * @property integer $trader_id
 * @property integer $trader_member_id
 * @property string $order_time
 * @property string $pay_time
 * @property string $finsh_time
 * @property string $create_at
 * @property string $update_at
 *
 * @property Product $product
 * @property User $provider
 * @property Member $providerMember
 * @property User $trader
 * @property Member $traderMember
 */
class Order extends \app\models\BaseModel {
    const STATUS_CREATE = 1;
    const STATUS_CANCEL = 2;
    const STATUS_FINISH = 3;
    const STATUS_REJECT = 4;
    public static $positions = [
        self::STATUS_CREATE => '待处理',
        self::STATUS_CANCEL => '已取消',
        self::STATUS_FINISH => '已完成',
        self::STATUS_REJECT => '已拒绝',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'order';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['product_id', 'unit_price', 'sale_num', 'order_price', 'import_duty', 'export_tax', 'status', 'provider_id', 'trader_id'], 'required'],
            [['product_id', 'sale_num', 'status', 'provider_id', 'provider_member_id', 'trader_id', 'trader_member_id'], 'integer'],
            [['unit_price', 'order_price', 'import_duty', 'export_tax'], 'number'],
            [['order_time', 'pay_time', 'finsh_time', 'create_at', 'update_at'], 'safe'],
            [['remark'], 'string', 'max' => 255],
            [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'id']],
            [['provider_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['provider_id' => 'id']],
            [['provider_member_id'], 'exist', 'skipOnError' => true, 'targetClass' => Member::className(), 'targetAttribute' => ['provider_member_id' => 'id']],
            [['trader_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['trader_id' => 'id']],
            [['trader_member_id'], 'exist', 'skipOnError' => true, 'targetClass' => Member::className(), 'targetAttribute' => ['trader_member_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'product_id' => 'Product ID',
            'unit_price' => 'Unit Price',
            'sale_num' => 'Sale Num',
            'order_price' => 'Order Price',
            'import_duty' => 'Import Duty',
            'export_tax' => 'Export Tax',
            'status' => 'Status',
            'remark' => '订单备注',
            'provider_id' => 'Provider ID',
            'provider_member_id' => 'Provider Member ID',
            'trader_id' => 'Trader ID',
            'trader_member_id' => 'Trader Member ID',
            'order_time' => 'Order Time',
            'pay_time' => 'Pay Time',
            'finsh_time' => 'Finsh Time',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    public function fields() {
        $fields = [
            'id',
            'userProduct',
            'unit_price',
            'sale_num',
            'order_price',
            'import_duty',
            'export_tax',
            'status',
            'remark',
            'provider',
            'providerMember',
            'trader',
            'traderMember',
            'order_time',
            'pay_time',
            'finsh_time',
        ];
        $fields = array_combine($fields, $fields);

        return $fields;
    }

    public function extraFields() {
        $fields = [
            'product_id',
            'provider_id',
            'provider_member_id',
            'trader_id',
            'trader_member_id',
            'create_at',
            'update_at',
        ];
        $fields = array_combine($fields, $fields);
        return $fields;
    }

    public function getUserProduct() {
        return $this->hasOne(UserProduct::className(), ['product_id' => 'product_id', 'user_id' => 'provider_id', ]);
    }

    public function getProduct() {
        return $this->hasOne(Product::className(), ['id' => 'product_id'])->inverseOf('orders');
    }

    public function getProvider() {
        return $this->hasOne(User::className(), ['id' => 'provider_id'])->inverseOf('orders');
    }

    public function getProviderMember() {
        return $this->hasOne(Member::className(), ['id' => 'provider_member_id'])->inverseOf('orders');
    }

    public function getTrader() {
        return $this->hasOne(User::className(), ['id' => 'trader_id'])->inverseOf('orders0');
    }

    public function getTraderMember() {
        return $this->hasOne(Member::className(), ['id' => 'trader_member_id'])->inverseOf('orders0');
    }
}
